package com.jinghang.WordCountApplication

import org.apache.spark.{SparkConf, SparkContext}

object WordCountOnSpark {

  def main(args: Array[String]): Unit = {

    println("args.length"+args.length)
    println("args.size"+args.size)

    if(args.size<2){
      println("参数有误")
    }

    val conf = new SparkConf()
      .setAppName("appName") //设置一个Application的id,用于在ui上辨别该Application
    val sparkContext = new SparkContext(conf)

    val rdd = sparkContext.textFile(args(0))
    val flatMapRdd = rdd.flatMap(line => line.split(" "))
    val mapRdd = flatMapRdd.map(x => (x, 1))
    val reduceByKeyRdd = mapRdd.reduceByKey((a, b) => (a + b)).sortByKey()

    reduceByKeyRdd.saveAsTextFile(args(1))

    //reduceByKeyRdd.collect().foreach(println)

    sparkContext.stop();
  }


}
